package com.icss.trans.ws.put;

import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.icss.trans.cfg.BaseProperties;

/**
 * 
 * @ClassName: WsSender
 * @Description:
 * @author ICSS
 * @date 2015-3-30 下午5:36:05
 * 
 */
public class WsSender {

    private static final Logger log = LoggerFactory.getLogger(WsSender.class);

    // 名字空间
    public static final String sendNamespace = "http://send.webservice.icssi.com/";

    // 服务名
    public static final String serName = "TransFileService";

    // 端口名
    public static final String pName = "TransFilePort";

    // 服务地址
    public static final String endpointAddress = "/TransFileService?wsdl";

    // 方法名
    public static final String OPER_NAME = "transFile";

    /**
     * @Description:
     * @param parameterObject
     * @param sourceAppcode
     *            源应用编码
     * @param destAppcode
     *            目标应用
     * @throws Exception
     */
    public static String sendData(WsSendParam parameterObject) throws Exception {

        QName serviceName = new QName(sendNamespace, serName);

        QName portName = new QName(sendNamespace, pName);

        javax.xml.ws.Service service = Service.create(serviceName);
        service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, BaseProperties.getInstance().getTransUrl()
                + endpointAddress);

        Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);

        BindingProvider bp = (BindingProvider) dispatch;
        Map<String, Object> rc = bp.getRequestContext();
        rc.put(BindingProvider.SOAPACTION_USE_PROPERTY, Boolean.TRUE);
        rc.put(BindingProvider.SOAPACTION_URI_PROPERTY, OPER_NAME);
        MessageFactory factory = ((SOAPBinding) bp.getBinding()).getMessageFactory();

        SOAPMessage request = factory.createMessage();
        SOAPBody body = request.getSOAPBody();
        QName payloadName = new QName(sendNamespace, OPER_NAME, "nsl");
        SOAPBodyElement payload = body.addBodyElement(payloadName);

        SOAPElement message4 = payload.addChildElement("sourceAppCode");
        message4.addTextNode(parameterObject.getSourceAppCode());
        SOAPElement message5 = payload.addChildElement("destOrgCode");
        message5.addTextNode(parameterObject.getDestOrgCode());
        SOAPElement message6 = payload.addChildElement("destAppCode");
        message6.addTextNode(parameterObject.getDestAppCode());
        SOAPElement message7 = payload.addChildElement("dataTransType");
        message7.addTextNode(parameterObject.getDataTransType());
        SOAPElement message2 = payload.addChildElement("key");
        message2.addTextNode(parameterObject.getKey());
        SOAPElement message8 = payload.addChildElement("fileName");
        message8.addTextNode(parameterObject.getFileName());
        SOAPElement message9 = payload.addChildElement("fileContent");
        message9.addTextNode(parameterObject.getFileContent());

        SOAPMessage reply = null;
        reply = dispatch.invoke(request);

        SOAPBody soapBody = reply.getSOAPBody();
        SOAPBodyElement nextSoapBodyElement = (SOAPBodyElement) soapBody.getChildElements().next();
        SOAPElement soapElement = (SOAPElement) nextSoapBodyElement.getChildElements().next();
        log.debug("WS back info is :" + soapElement.getValue());
        return soapElement.getValue();
    }

}
